package com.yoomiito.app.ui.college.list

import android.graphics.Typeface
import android.graphics.drawable.GradientDrawable
import android.text.Spannable
import android.text.SpannableString
import android.text.Spanned
import android.text.style.*
import android.widget.ImageView
import android.widget.TextView
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.yoomiito.app.R
import com.yoomiito.app.model.colleage.Subject
import com.yoomiito.app.utils.DensityUtil
import com.yoomiito.app.utils.ImageLoader
import com.yoomiito.app.utils.ResUtil

class CollegeSubjectAdapter(s: List<Subject>?) : BaseQuickAdapter<Subject, BaseViewHolder>(R.layout.item_college, s) {

    override fun convert(helper: BaseViewHolder?, item: Subject?) {
        helper?.getView<ImageView>(R.id.videoPic).let {
//            ImageLoader.getInstance().intoAllSize(mContext,item?.product_img,it,R.drawable.default_long_pic)
            ImageLoader.getInstance().into(mContext,item?.product_img,it,R.drawable.default_long_pic)

        }
        helper?.getView<TextView>(R.id.videoTitleTv)?.text = item?.product_name
        helper?.getView<TextView>(R.id.videoPriceTv).let {
            it?.text = ""
            it?.text = if (item!!.isHas_buy) textStyle("已购买") else  textStyle("¥${item.sale_price} ¥${item.market_price}")
        }

        helper?.getView<TextView>(R.id.videoStatusBtn).let {
            it?.text = if (item!!.isHas_buy) "去学习" else "立即购买"
            it?.background = background(if (item!!.isHas_buy) R.color.color_73BC82 else R.color.color_FF035B)
        }
    }

    private fun textStyle(content:String):SpannableString{
        val spanText = SpannableString(content)
        if (content.startsWith("¥")){
            val pricePosition = content.indexOf(" ")

            spanText.setSpan(RelativeSizeSpan(1.1f), 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            spanText.setSpan(StyleSpan(Typeface.BOLD), 1, pricePosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spanText.setSpan(RelativeSizeSpan(2f), 1, pricePosition, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            spanText.setSpan(ScaleXSpan(DensityUtil.dp2px(1f).toFloat()),pricePosition, pricePosition+1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)

            spanText.setSpan(ForegroundColorSpan(ResUtil.getColor(R.color.color_balk_999999)), pricePosition+1, content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spanText.setSpan(StrikethroughSpan(), pricePosition+1, content.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)

        }else{
            spanText.setSpan(RelativeSizeSpan(1.5f), 0, content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
            spanText.setSpan(StyleSpan(Typeface.BOLD), 0, content.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE)
        }
        return spanText
    }

    private fun background(colorRes: Int): GradientDrawable {
        val gd = GradientDrawable()
        gd.cornerRadius = DensityUtil.dp2px(16f).toFloat()
        gd.setColor(ResUtil.getColor(colorRes))
        return gd
    }

}